<?php
#基础函数
include_once('_path.php');

include_once(_ROOTPATH."backend/includes/init.php");

if ($_POST["do"] == "do_note") {
	$note = trim($_REQUEST['note']);
    $quote['note'] = string_sql($note);
  verify_field("note", "Note", "maxlength", 10000);

	if (!count($error_strs)) {
		$sql = "UPDATE setting SET note = ".$quote['note']." WHERE id = 1;";
    $database->execute($sql);

    add_log('do_note', 'index', 0, 0, 0, 0, 0);

		$_REQUEST['referrer'] = "index.php";
		redirect("Saved");
    die();
	}
}

#定义含义
$type_meaning = array('0' => "Unknown", '1' => "Japanese Manga", '2' => "Korean Manhwa", '3' => "Chinese Manhua");
$direction_meaning = array('' => "Unknown", 'rl' => "Read from right to left", 'lr' => "Read from left to right");


$sql = "SELECT note FROM setting WHERE id = 1";
$setting = $database->queryFirst($sql);

#总计漫画
$sql = "SELECT count(*) as total FROM manga_series WHERE total_chapters > 0 AND is_online = 1";
$rs = $database->queryArray($sql, rand(3600,7200));
$total_sreies = $rs[0]["total"];
#总计章节
$sql = "SELECT count(is_online) as total FROM manga_chapters USE INDEX (is_online)";
$rs = $database->queryArray($sql, rand(3600,7200));
$total_chapters = $rs[0]["total"];
#总计页数
$sql = "SELECT count(counter) as total FROM manga_scans USE INDEX (counter)";
$rs = $database->queryArray($sql, rand(3600,7200));
$total_scans = $rs[0]["total"];
#待处理的上传内容
$sql = "SELECT fw.*, ms.name series_name, ms.note series_note, uu.username uploader_name, count(mc1.uploader_id) total_uploaded, mc2.chapter_id existed_id ".
"FROM file_work fw LEFT JOIN manga_series ms ON fw.series_id = ms.series_id LEFT JOIN ".
_CONST_VB_PREFIX."user uu ON fw.uploader_id = uu.userid LEFT JOIN ".
"manga_chapters mc1 ON fw.uploader_id = mc1.uploader_id LEFT JOIN manga_chapters mc2 ON fw.series_id = mc2.series_id AND fw.chapter_no = mc2.no WHERE status = 'T' GROUP BY fw.work_id ORDER BY fw.entry_time DESC LIMIT 0, 10";
$rs_files = $database->queryArray($sql);
#待处理的series wiki
$sql = "SELECT sw.*, ms.name org_name, ms.altnames org_altnames, ms.author org_author, ".
"ms.artist org_artist, ms.genres org_genres, ms.summary org_summary, ms.`type` org_type, ms.direction org_direction, ".
"ms.released org_released, ms.is_completed org_is_completed, ru.username requester_name, ".
"mu.username moderator_name ".
"FROM series_work sw LEFT JOIN "._CONST_VB_PREFIX."user ru ON ru.userid = sw.requester_id ".
"LEFT JOIN "._CONST_VB_PREFIX."user mu ON mu.userid = sw.moderator_id ".
"LEFT JOIN manga_series ms ON ms.series_id = sw.series_id ".
"WHERE status = 'N'ORDER BY sw.entry_time DESC LIMIT 0, 10";
$rs_series = $database->queryArray($sql);

#待处理的chapter wiki
$sql = "SELECT cw.*, ms.series_id, ms.name series_name, mc.name org_name, mc.scanlator org_scanlator, ".
"mc.no org_no, ru.username requester_name, mu.username moderator_name ".
"FROM chapter_work cw ".
"LEFT JOIN "._CONST_VB_PREFIX."user ru ON ru.userid = cw.requester_id ".
"LEFT JOIN "._CONST_VB_PREFIX."user mu ON mu.userid = cw.moderator_id ".
"LEFT JOIN manga_chapters mc ON mc.chapter_id = cw.chapter_id ".
"LEFT JOIN manga_series ms ON ms.series_id = mc.series_id ".
"WHERE status = 'N'ORDER BY cw.entry_time DESC LIMIT 0, 10";
$rs_chapter = $database->queryArray($sql);

$css = array('index', 'table');
$js = array();

add_log('show', 'index');

include_once(_ROOTPATH."backend/layout/_header.php");
?>
<div id="path">
	<ul>
		<li><a href="<?= _CONST_WEB_URL ?>">Manga Fox</a></li>
		<li><a href="<?= _CONST_BACKEND_URL ?>">Backend</a></li>
		<li>Dashboard</li>
	</ul>
	<a href="<?=_CONST_VB_URL?>login.php?do=logout&logouthash=<?= $user['logouthash'] ?>" class="r" style="margin:2px 10px"><span class="logout">Logout</span></a>
</div>

<div id="content">

	<ul id="submenu">
		<li class="current"><a href="<?= _CONST_BACKEND_URL ?>index.php" class="button"><span class="dashboard">Dashboard</span></a></li>
		<li><a href="<?= _CONST_BACKEND_URL ?>manga/index.php" class="button"><span class="manga">Manga Series</span></a></li>
		<li><a href="<?= _CONST_BACKEND_URL ?>groups/group.php" class="button"><span class="group">Scanlation Groups</span></a></li>
    <? if ($is_administrator) { ?>
		<li><a href="<?= _CONST_BACKEND_URL ?>setting/index.php" class="button"><span class="setting">Setting</span></a></li>
		<? } ?>
                <li><a href="<?= _CONST_BACKEND_URL ?>catch/index.php" class="button"><span class="setting">Catch Manage</span></a></li>
    <li><a href="<?= _CONST_BACKEND_URL ?>logs/index.php" class="button"><span class="log">Logs</span></a></li>
	</ul>
  <span style="margin:7px 10px 2px 50px;" class="l"><?= date('D  M j, Y h:ia T') ?></span>

		<div class="border cr">
			<!-- Statistics -->
			<div style="float:left">
      <h2 style="margin:10px;">Statistics</h2>
			<table cellspacing="4" class="info l cr">
				<tr>
					<td><b>Total Series</b></td>
					<td class="rt">
						<a href="<?= _CONST_BACKEND_URL ?>manga/series"><?=$total_sreies?></a>
					</td>
				</tr>
				<tr>
					<td><b>Total Chapters</b></td>
					<td class="rt">
					<?=$total_chapters?>
					</td>
				</tr>
				<tr>
					<td><b>Total Pages</b></td>
					<td class="rt"><?=$total_scans ?></td>
				</tr>
			</table>
      </div>
      <div style="float:right;margin-left:10px;width:605px;">
      <form action="index.php" method="post">
      <input type="hidden" name="do" value="do_note" />
      <div><h2 style="margin:10px;">Note</h2><input type="submit" value="save" style="margin: 10px; float: right;" /></div>
      <div class="cr"></div>
      <textarea name="note" id="note" rows="3" cols="50" style="font-size:11px;height:80px;width:600px;"><?= htmlspecialchars($setting['note']) ?></textarea>
      </form>
      </div>
			<div class="cr"></div>

			<!-- Uploaded Files Moderation -->
			<h2 style="margin:10px;">
				<a href="work/files.php" style="color:#0189C7;">Uploaded Files Moderation</a>
			</h2>
			<?
			if (sizeof($rs_files)> 0) {
				?>
				<div class="cr"></div>
				<form action="work/file_edit.php?action=multiple.do" name="fileform" id="fileform" method="post">
				<input type="hidden" name="type" value="" />
				<table class="list" cellspacing="0">
					<tr>
						<th class="no">&nbsp;</th>
						<th class="no">ID</th>
						<th width="50%" class="no">Series Name</th>
						<th width="50%" class="no">Chapter Name</th>
						<th class="no">No</th>
						<th class="no">File Name</th>
						<th class="no">File Size</th>
						<th class="no">Entry Date</th>
						<th class="no">Upload By</th>
						<th class="no">&nbsp;</th>
					</tr>
					<?
					for($i=0;$i<sizeof($rs_files);$i++){
						$r = $rs_files[$i];
						?>
            <tr>
              <td class="no"><input type="checkbox" name="work_ids[]" value="<?= $r['work_id'] ?>"></td>
              <td class="no"><?= $r['work_id'] ?></td>
              <td><a href="../manga/chapters.php?series_id=<?= $r['series_id'] ?>" title="<?= htmlspecialchars($r['comment']) ?>"><?= $r['series_name'] ?></a><br/><span class="ss"><?= htmlspecialchars($r['series_note']) ?></span></td>
              <td><?= iif($r['chapter_name'] != "", "<a href=\"work/file_edit.php?action=edit.form&work_id=".$r['work_id']."\">".$r['chapter_name']."</a>", "n/a") ?></td>
              <td class="no" <?= iif($r['existed_id'] > 0, "style=\"background-color:#FF9999\"") ?>><a href="work/file_edit.php?action=edit.form&work_id=<?= $r['work_id'] ?>"><?= reformat_volch($r['chapter_no']) ?></a></td>
              <td class="no"><?= iif($r['file_name'] != '', $r['file_name'], 'n/a') ?><?= iif($r['scanlator'] != '', "<br/><span class=\"ss\">by ".htmlspecialchars($r['scanlator']))."</span>" ?></td>
              <td class="no" <?= iif($r['file_size'] < 102400, "style=\"background-color:#FF9999\"") ?>><?= file_size($r['file_size']) ?></td>
              <td class="no"><?= convert_date($r['entry_time']) ?></td>
              <td><?= iif($r['uploader_name'] != "", "<a href=\""._CONST_BACKEND_URL."user.php?user_id=".$r['uploader_id']."\">".$r['uploader_name']."</a> (".intval($r['total_uploaded']).")", "n/a") ?><br/><span class="ss"><?= htmlspecialchars($r['comment']) ?></span></td>
              <td class="action no">
								<a href="work/file_edit.php?action=edit.form&work_id=<?= $r['work_id'] ?>" class="button"><img src="<?= _CONST_BACKEND_URL ?>media/edit.png" /> Edit</a> &nbsp;
								<a href="work/file_edit.php?action=accept.do&work_id=<?= $r['work_id'] ?>" class="button"><img src="<?= _CONST_BACKEND_URL ?>media/accept.png" /> Accept</a> &nbsp;
								<a href="work/file_edit.php?action=delete.do&work_id=<?= $r['work_id'] ?>" onclick="return warning();" class="button"><img src="<?= _CONST_BACKEND_URL ?>media/delete.png" /> Delete</a>
							</td>
            </tr>
						<?
					} // while ($r = $st->get_row())
					?>
					<tr><td colspan="10" class="action" style="padding:5px;">
						<a href="javascript:document.fileform.type.value='accept';document.fileform.submit();" class="button"><span class="accept">Accept</span></a> &nbsp; &nbsp; &nbsp; &nbsp;
						<a href="javascript:document.fileform.type.value='delete';document.fileform.submit();" onclick="return warning();" class="button"><span class="delete">Delete</span></a>
					</td></tr>
				</table>
				</form>
				<?
			} else {
				?><div class="border cr">No Record</div><?
			} // if ($st->num_rows() > 0)
			?>

			<style>
				table.compare {
					width: 100%;
					border-collapse: collapse;
					border-spacing: 0;
					background: #eee;
				}
				table.compare tr {
					background: #fff;
				}
				table.compare tr:hover {
					background: #eee;
				}
				table.compare th {
					padding: 2px 3px;
					font-size: 10px;
					background: #ddd;
					border: 1px solid #ccc;
          color: #999;
				}
				table.compare td {
					font-size: 10px;
					vertical-align: top;
					padding: 2px 3px;
					border: 1px solid #ccc;
				}
				table.compare tr.diff {
					background:#FFCCFF;
				}
			</style>

			<!-- Series Modification Requests -->
			<div class="cr"></div>
			<h2 style="margin:10px;"><a href="work/series.php" style="color:#0189C7;">Series Modification Requests</a></h2>
			<?
			if (sizeof($rs_series)> 0) {
				?>
				<div class="cr"></div>
				<form action="work/series.php?action=multiple.do" name="seriesform" id="seriesform" method="post">
				<input type="hidden" name="type" value="" />
				<table class="list" cellspacing="0">
					<tr>
						<th class="no">&nbsp;</th>
						<th class="no">ID</th>
						<th width="50%" class="no">Name</th>
						<th class="no">Entry Date</th>
						<th class="no">Requested By</th>
						<th class="no">&nbsp;</th>
					</tr>
					<?
					for($i=0;$i<sizeof($rs_series);$i++){
						$r = $rs_series[$i];
						?>
						<tr>
							<td class="no"><? if ($r['status'] == 'N') { ?><input type="checkbox" name="work_ids[]" value="<?= $r['work_id'] ?>"><? } ?></td>
							<td class="no"><?= $r['work_id'] ?></td>
							<td><a href="manga/manga_edit.php?func=edit&series_id=<?= $r['series_id'] ?>"><?= $r['org_name'] ?></a></td>
							<td class="no"><?= convert_date($r['entry_time']) ?></td>
							<td class="no"><?= iif($r['requester_id'] != "", "<a href=\""._CONST_BACKEND_URL."user.php?user_id=".$r['requester_id']."\">".$r['requester_name']."</a>", "n/a") ?></td>
							<td class="action no">
								<? if ($r['status'] == 'N') { ?>
									<a href="work/series.php?action=accept.do&work_id=<?= $r['work_id'] ?>" class="button"><img src="<?= _CONST_BACKEND_URL ?>media/accept.png"> Accept</a> &nbsp;
									<a href="work/series.php?action=delete.do&work_id=<?= $r['work_id'] ?>" class="button"><img src="<?= _CONST_BACKEND_URL ?>media/delete.png"> Delete</a>
								<? } ?>
							</td>
						</tr>
						<tr>
							<td class="no">&nbsp;</td>
							<td colspan="5" style="font-size:10px">
								<table class="compare" cellspacing="0">
									<tr>
										<th>&nbsp;</th><th width="50%" class="org" colspan="2">Original</th><th width="50%" class="new" colspan="2">New</th>
									</tr>
									<tr class="<?= iif($r['org_name'] != $r['name'], "diff") ?>">
										<td><b>Name:</b></td><td width="50%" class="org"><?= htmlspecialchars($r['org_name']) ?></td><td rowspan="6" style="background:#fff" class="org"><? if (file_exists(_CONST_MANGA_PATH."/".$r['series_id']."/cover.jpg")) { ?>
											<img width="75" src="<?= _CONST_MANGA_URL."/".$r['series_id']."/cover.jpg" ?>" onmouseover="showtrail(this.src,200,200)" onmouseout="hidetrail()" style="border:1px dotted #999;" />
										<? } ?></td><td width="50%" class="new"><?= htmlspecialchars($r['name']) ?></td><td rowspan="6" style="background:#fff" class="new"><? if (file_exists(_CONST_UPLOAD_PATH."/".$r['series_id'].".cover.".$r['work_id'].".jpg")) { ?>
											<img width="75" src="<?= _CONST_UPLOAD_URL."/".$r['series_id'].".cover.".$r['work_id'].".jpg" ?>" onmouseover="showtrail(this.src,200,200)" onmouseout="hidetrail()" style="border:1px dotted #999;" />
										<? } ?></td>
									</tr>
									<tr class="<?= iif($r['org_altnames'] != $r['altnames'], "diff") ?>">
										<td><b>Alternative Series Name(s):</b></td><td class="org"><?= nl2br(htmlspecialchars($r['org_altnames'])) ?></td><td class="new"><?= nl2br(htmlspecialchars($r['altnames'])) ?></td>
									</tr>
									<tr class="<?= iif($r['org_author'] != $r['author'], "diff") ?>">
										<td><b>Author(s):</b></td><td class="org"><?= htmlspecialchars($r['org_author']) ?></td><td class="new"><?= htmlspecialchars($r['author']) ?></td>
									</tr>
									<tr class="<?= iif($r['org_artist'] != $r['artist'], "diff") ?>">
										<td><b>Artist(s):</b></td><td class="org"><?= htmlspecialchars($r['org_artist']) ?></td><td class="new"><?= htmlspecialchars($r['artist']) ?></td>
									</tr>
									<tr class="<?= iif($r['org_released'] != $r['released'], "diff") ?>">
										<td><b>Released:</b></td><td class="org"><?= htmlspecialchars($r['org_released']) ?></td><td class="new"><?= htmlspecialchars($r['released']) ?></td>
									</tr>
									<tr class="<?= iif($r['org_genres'] != $r['genres'], "diff") ?>">
										<td><b>Genre(s):</b></td><td class="org"><?= htmlspecialchars($r['org_genres']) ?></td><td class="new"><?= htmlspecialchars($r['genres']) ?></td>
									</tr>
                  <tr class="<?= iif($r['org_summary'] != $r['summary'], "diff") ?>">
										<td><b>Summary:</b></td><td class="org" colspan="4"><?= nl2br(htmlDiff(htmlspecialchars($r['org_summary']), htmlspecialchars($r['summary']))) ?></td>
									</tr>
									<tr class="<?= iif($r['org_type'] != $r['type'], "diff") ?>">
										<td><b>Type:</b></td><td class="org" colspan="2"><?= $type_meaning[$r['org_type']] ?></td><td class="new" colspan="2"><?= $type_meaning[$r['type']] ?></td>
									</tr>
									<tr class="<?= iif($r['org_direction'] != $r['direction'], "diff") ?>">
										<td><b>Reading Direction:</b></td><td class="org" colspan="2"><?= $direction_meaning[$r['org_direction']] ?></td><td class="new" colspan="2"><?= $direction_meaning[$r['direction']] ?></td>
									</tr>
                  <tr class="<?= iif($r['org_is_completed'] != $r['is_completed'], "diff") ?>">
										<td><b>Completed:</b></td><td class="org" colspan="2"><?= iif($r['org_is_completed'],"yes","no") ?></td><td class="new" colspan="2"><?= iif($r['is_completed'],"yes","no") ?></td>
									</tr>
								</table>
							</td>
						</tr>
						<?
					} // while ($r = $st->get_row())
					?>
					<tr><td colspan="11" class="action" style="padding:5px;">
						<a href="javascript:document.seriesform.type.value='accept';document.seriesform.submit();" class="button"><span class="accept">Accept</span></a> &nbsp; &nbsp; &nbsp; &nbsp;
						<a href="javascript:document.seriesform.type.value='delete';document.seriesform.submit();" onclick="return warning();" class="button"><span class="delete">Delete</span></a>
					</td></tr>
				</table>
				</form>
				<script type="text/javascript" src="<?= _CONST_BACKEND_URL ?>media/js/preview.js"></script>
				<div style="display:none;position:absolute;z-index:110;" id="preview_div"></div>
				<?
			} else {
				?><div class="border cr">No Record</div><?
			} // if ($st->num_rows() > 0)


			?>
			<div class="cr"></div>
			<h2 style="margin:10px;"><a href="work/chapters.php" style="color:#0189C7;">Chapter Modification Requests</a></h2>
			<?
			if (sizeof($rs_chapter) > 0) {
				?>
				<div class="cr"></div>
				<form action="work/chapters.php?action=multiple.do" name="chapterform" id="chapterform" method="post">
				<input type="hidden" name="type" value="" />
				<table class="list" cellspacing="0">
					<tr>
						<th class="no">&nbsp;</th>
						<th class="no">ID</th>
						<th width="50%" class="no">Name</th>
						<th class="no">Entry Date</th>
						<th class="no">Requested By</th>
						<th class="no">&nbsp;</th>
					</tr>
					<?
					for($i=0;$i<sizeof($rs_chapter);$i++){
						$r = $rs_chapter[$i];
					//while ($r = $st->get_row()) {
						list($org_vol, $org_no) = unpack_volch($r['org_no']);
						list($vol, $no) = unpack_volch($r['no']);
						?>
						<tr>
							<td class="no"><? if ($r['status'] == 'N') { ?><input type="checkbox" name="work_ids[]" value="<?= $r['work_id'] ?>"><? } ?></td>
							<td class="no"><?= $r['work_id'] ?></td>
							<td>
								<a href="manga/manga_edit.php?func=edit&series_id=<?= $r['series_id'] ?>"><?= htmlspecialchars($r['series_name']) ?></a>,
								<a href="manga/chapter_edit.php?func=edit&series_id=<?= $r['series_id'] ?>&chapter_id=<?= $r['chapter_id'] ?>"><?= reformat_volch($r['org_no']).iif($r['org_name'], ": ".htmlspecialchars($r['org_name'])) ?></a>
							</td>
							<td class="no"><?= convert_date($r['entry_time']) ?></td>
							<td class="no"><?= iif($r['requester_id'] != "", "<a href=\""._CONST_BACKEND_URL."user.php?user_id=".$r['requester_id']."\">".$r['requester_name']."</a>", "n/a") ?></td>
							<td class="action no">
								<? if ($r['status'] == 'N') { ?>
									<a href="work/chapters.php?action=accept.do&work_id=<?= $r['work_id'] ?>" class="button"><img src="<?= _CONST_BACKEND_URL ?>media/accept.png"> Accept</a> &nbsp;
									<a href="work/chapters.php?action=delete.do&work_id=<?= $r['work_id'] ?>" class="button"><img src="<?= _CONST_BACKEND_URL ?>media/delete.png"> Delete</a>
								<? } ?>
							</td>
						</tr>
						<tr>
							<td class="no">&nbsp;</td>
							<td colspan="5" style="width:100%">
								<table class="compare" cellspacing="0">
									<tr>
										<th>&nbsp;</th><th width="50%" class="org">Original</th><th width="50%" class="new">New</th>
									</tr>
									<tr class="<?= iif($r['org_name'] != $r['name'], "diff") ?>">
										<td><b>Name:</b></td><td width="50%" class="org"><?= htmlspecialchars($r['org_name']) ?></td><td width="50%" class="new"><?= htmlspecialchars($r['name']) ?></td>
									</tr>
									<tr class="<?= iif($org_vol != $vol, "diff") ?>">
										<td><b>Volume:</b></td><td class="org"><?= htmlspecialchars($org_vol) ?></td><td class="new"><?= htmlspecialchars($vol) ?></td>
									</tr>
									<tr class="<?= iif($org_no != $no, "diff") ?>">
										<td class="no"><b>Chapter No:</b></td><td class="org"><?= htmlspecialchars($org_no) ?></td><td class="new"><?= htmlspecialchars($no) ?></td>
									</tr>
									<tr class="<?= iif($r['org_scanlator'] != $r['scanlator'], "diff") ?>">
										<td><b>Scanlator:</b></td><td class="org"><?= htmlspecialchars($r['org_scanlator']) ?></td><td class="new"><?= htmlspecialchars($r['scanlator']) ?></td>
									</tr>
								</table>
							</td>
						</tr>
						<?
					} // while ($r = $st->get_row())
					?>
					<tr><td colspan="10" class="action" style="padding:5px;">
						<a href="javascript:document.chapterform.type.value='accept';document.chapterform.submit();" class="button"><span class="accept">Accept</span></a> &nbsp; &nbsp; &nbsp; &nbsp;
						<a href="javascript:document.chapterform.type.value='delete';document.chapterform.submit();" onclick="return warning();" class="button"><span class="delete">Delete</span></a>
					</td></tr>
				</table>
				</form>
				<?
			} else {
				?><div class="border cr">No Record</div><?
			} // if ($st->num_rows() > 0)
			?>
		</div>
</div>
<?
include_once(_ROOTPATH."backend/layout/_footer.php");
?>